package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.ContractProduct;
import cn.itcast.domain.cargo.ContractProductExample;
import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.service.cargo.ContractProductService;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/cargo/contractProduct")
public class ContractProductController extends BaseController {

    @Reference
    private ContractProductService contractProductService;

    @Reference
    private FactoryService factoryService;

    @RequestMapping("/list")
    public String list(String contractId, @RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize) {

        //1. 查询当前购销合同下的货物数据,得到pageInfo

        ContractProductExample contractProductExample = new ContractProductExample();

        //由于我们只需要查询当前购销合同的货物

        contractProductExample.createCriteria().andContractIdEqualTo(contractId);

        PageInfo<ContractProduct> pageInfo = contractProductService.findByPage(contractProductExample, pageNum, pageSize);

        request.setAttribute("pageInfo", pageInfo);

        //2. 查询生成货物的厂家
        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("货物");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList", factoryList);

        //3. 把购销合同id存储到域中

        request.setAttribute("contractId", contractId);

        return "cargo/product/product-list";


    }

      /*
    作用：添加货物，更新货物
    url:  /contractProduct/edit.do
    参数：ContractProduct 货物对象信息
    返回值 : 货物列表页面
 */

    @RequestMapping("/edit")
    public String edit(ContractProduct contractProduct) {
        //货物的创建人
        contractProduct.setCreateBy(getLoginUser().getId());

        //货物的创建人所属的部门

        contractProduct.setCreateDept(getLoginUser().getCompanyId());

        //货物的创建人所属的企业id
        contractProduct.setCompanyName(getLoginUserCompanyName());

        //货物的创建人所属的企业名称

        contractProduct.setCompanyName(getLoginUserCompanyName());

        if (StringUtils.isEmpty(contractProduct.getId())) {//判断一个变量是否为空串,相当于上面的语句

            //添加
            contractProductService.save(contractProduct);

        } else {
            //更新
            contractProductService.update(contractProduct);
        }
        return "redirect:/cargo/contractProduct/list.do?contractId=" + contractProduct.getContractId();  //访问上面的list方法

    }

        /*
         作用 ： 进入更新货物的页面
         url : /cargo/contractProduct/toUpdate.do?id=093226d4-24a2-4deb-90d5-5a2fadea10a9
        参数 : 货物id
        返回值 :product-update
     */


    @RequestMapping("/toUpdate")
    public String toUpdate(String id) {
        //1. 根据货物的id查找货物
        ContractProduct contractProduct = contractProductService.findById(id);

        //2. 存储到request域中
        request.setAttribute("contractProduct", contractProduct);


        //3. 生成货物的厂家
        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("货物");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList", factoryList);

        //4. 请求转发到product-update页面
        return "cargo/product/product-update";

    }


    @RequestMapping("/delete")
    public String delete(String id, String contractId) {
        contractProductService.delete(id);
        return "redirect:/cargo/contractProduct/list.do?contractId=" + contractId;
    }


    /*
        作用 ：进入上传货物的页面
        url :/cargo/contractProduct/toImport.do?contractId=902567ff-1ba5-4258-bccb-9e18586569e4
       参数 : 购销合同的id
       返回值 : 进入上传货物的页面
    */

    @RequestMapping("/toImport")
    public String toImport(String contractId) {
        request.setAttribute("contractId", contractId);
        return "cargo/product/product-import";
    }

    /*
     作用 ：保存上传货物数据
     url :/cargo/contractProduct/import.do
    参数 : 购销合同的id,excel文件
    返回值 : 货物列表
 */
    @RequestMapping("/import")
    public String importExcel(String contractId, MultipartFile file) throws IOException {

        //1.创建工作薄，读取上传文件
        Workbook workbook = new XSSFWorkbook(file.getInputStream());
        //2. 得到工作单
        Sheet sheet = workbook.getSheetAt(0);

        //3. 得到文件所有行数

        int rows = sheet.getPhysicalNumberOfRows();

        //4. 遍历所有行，读取excel内容, 每一行数据对应ContractProduct.
        for (int index = 1; index < rows; index++) {

            Row row = sheet.getRow(index);

            //每一行对应一个ContractProduct
            ContractProduct contractProduct = new ContractProduct();

            //生成厂家
            if (row.getCell(1) != null) {
                String factoryName = row.getCell(1).getStringCellValue();
                contractProduct.setFactoryName(factoryName);

            }

            //货号
            if (row.getCell(2) != null) {
                String productNo = row.getCell(2).getStringCellValue();
                contractProduct.setProductNo(productNo);

            }


            //数量
            if (row.getCell(3) != null) {
                double cnumber = row.getCell(3).getNumericCellValue();
                contractProduct.setCnumber((int) cnumber);

            }

            //包装单位
            if (row.getCell(4) != null) {
                String packingUnit = row.getCell(4).getStringCellValue();
                contractProduct.setPackingUnit(packingUnit);

            }

            //装率
            if (row.getCell(5) != null) {
                double loadingRate = row.getCell(5).getNumericCellValue();
                contractProduct.setLoadingRate(loadingRate + "");

            }

            //箱数
            if (row.getCell(6) != null) {
                double boxNum = row.getCell(6).getNumericCellValue();
                contractProduct.setBoxNum((int) boxNum);

            }

            //单价
            if (row.getCell(7) != null) {
                double price = row.getCell(7).getNumericCellValue();
                contractProduct.setPrice(price);

            }


            //货物描述
            if (row.getCell(8) != null) {
                String productDesc = row.getCell(8).getStringCellValue();
                contractProduct.setProductDesc(productDesc);

            }

            //要求
            if (row.getCell(9) != null) {
                String request = row.getCell(9).getStringCellValue();
                contractProduct.setProductRequest(request);

            }


            //补充数据
            //货物所属的购销合同
            contractProduct.setContractId(contractId);

            //该货物所属的生产厂家的id
            Factory factory = factoryService.findByFactoryName(contractProduct.getFactoryName());

            contractProduct.setFactoryId(factory.getId());

            //保存数据
            contractProductService.save(contractProduct);


        }

        //返回货物列表页面
        return "redirect:/cargo/contractProduct/list.do?contractId=" + contractId;
    }

}




















